package com.enjoy.concurrent.ch10.future;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String result = "success";
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            try {
                System.out.println("开始执行");
                Thread.sleep(3000);
                System.out.println("执行结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, result);

        new Thread(futureTask).start();


        System.out.println("futureTask = " + futureTask.get());
    }

    @Test
    public void test1() throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            System.out.println("开始执行");
            Thread.sleep(3000);
            System.out.println("执行结束");
            return "success";
        });

        new Thread(futureTask).start();

        System.out.println("futureTask.get() = " + futureTask.get());
    }
}
